package org.hackystat.devcathlon.ui.page.matches;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.hackystat.devcathlon.engine.match.Match;
import org.hackystat.devcathlon.engine.match.MatchManager;

/**
 * Provide Session state for the Matches area of the Devcathlon application. An instance of this is
 * automatically created in DevcathlonSession. Access using
 * DevcathlonSession.get().getMatchesSession().
 * 
 * @author Philip Johnson, John Zhou
 */
public class MatchesSession implements Serializable {

  /** Support serialization. */
  private static final long serialVersionUID = 1L;

  /** The selected match. */
  private String selectedMatch = "";
  /** Content of a comment. */
  private String content;

  /**
   * Support session state for the Matches page and subpages.
   */
  public MatchesSession() {
    //Nothing yet
  }

  /**
   * Get list of matches for MatchesSessions.
   * 
   * @return listItem The list of matches.
   */
  public List<Match> getUserMatches() {
    return new ArrayList<Match>(MatchManager.getInstance().getAllMatches());
  }
  
  /**
   * Get list of match names.
   * @return The list of match names. 
   * 
   */
  public List<String> getMatchNames() {
    return new ArrayList<String>(MatchManager.getInstance().getMatchNames());
  }
  
  /**
   * Get list of matches.
   * @return The list of matches.
   */
  public List<Match> getMatches() {
    return new ArrayList<Match>(MatchManager.getInstance().getAllMatches());
  }
  
  /**
   * Sets the selected match.
   * @param name The selected match.
   */
  public void setSelectedMatch(String name) {
    this.selectedMatch = name;    
  }
  
  /**
   * Sets the selected match to blank fields.
   */
  public void clearSelectedMatch() {
    this.selectedMatch = "";
  }
  
  /**
   * Gets the selected match.
   * @return The selected match.
   */
  public String getSelectedMatch() {
    return this.selectedMatch;
  }
  
  /**
   * Gets the match specified.
   * @param name The name of the match.
   * @return The match.
   */
  public Match getMatch(String name) {
    return MatchManager.getInstance().getMatch(name);
  }

  /**
   * Sets the content of a comment.
   * @param content The content to set.
   */
  public void setContent(String content) {
    this.content = content;
  }

  /**
   * Gets the content of a comment.
   * @return content The content of a comment.
   */
  public String getContent() {
    return content;
  }
  
  /**
   * Takes in a list of matches and returns the match names only.  
   * Needed to avoid serialization in ListView and DataViews.  Prevents
   * from storing the Match instance in its component model (?).
   * @param matches The list of matches.
   * @return The list of match names.
   */
  public static List<String> convertToMatchNames(List<Match> matches) {
    List<String> matchNames = new ArrayList<String>();
    for (Match match : matches) {
      matchNames.add(match.getName());
    }
    return matchNames;
  }
 
}
